package com.redrose.flutterdemo.ui.activitys;

import android.content.Context;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import com.alibaba.android.arouter.launcher.ARouter;
import com.redrose.flutterdemo.R;
import com.redrose.flutterdemo.databinding.ActivityBaseBinding;
import com.redrose.flutterdemo.utils.Utils;

import me.yokeyword.fragmentation.SupportActivity;

/**
 * Desc:
 *
 * @author: RedRose
 * Date: 2019/5/19 0019
 * Email: yinsxi@163.com
 */
public abstract class BaseActivity<T extends ViewDataBinding> extends SupportActivity {
    protected T mBinding;
    protected  final String TAG = getClass().getCanonicalName();
    public abstract void initData();
    protected Context mContext;
    private ActivityBaseBinding mBaseBinding;
    /**
     * 是否旋转图片
     * true 表示当前Activity 处于横屏，false：当前处于竖屏
     */
    protected boolean mIsRotate = false;
    public abstract int setLayout();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        mContext = this;
        mBaseBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_base, null, false);
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), setLayout(), null, false);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mBinding.getRoot().setLayoutParams(params);
        mBaseBinding.container.addView(mBinding.getRoot());
        getWindow().setContentView(mBaseBinding.getRoot());
        setTitleBar();
        initData();
    }

    protected void setTitleBar() {
        setSupportActionBar(mBaseBinding.toolBar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            //去除默认Title显示
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(false);
        }
        mBaseBinding.backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    //如果为true,则是键盘正在显示
                    if (imm.isActive()) {
                        //软件盘处于显示状态
                        //隐藏键盘
                        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow
                                (BaseActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                onBackPressed();
            }
        });
    }

    protected void hideToolBar() {
        mBaseBinding.toolBar.setVisibility(View.GONE);
    }

    protected void hideBack() {
        mBaseBinding.backBtn.setVisibility(View.GONE);
    }

    /**
     * 设置标题
     *
     * @param text
     */
    public void setTitle(CharSequence text) {
        mBaseBinding.toolBar.setTitle("");
        mBaseBinding.titleTv.setText(text);
    }

    /**
     * 设置标题栏右侧文字
     *
     * @param text
     */
    public void setRightTitle(CharSequence text) {
        mBaseBinding.titlebarRightTv.setText(text);
    }

    public void setLeftTitle(CharSequence text) {
        mBaseBinding.leftTitleTv.setText(text);
    }

    public void setToolbarTitle(CharSequence text) {
        //取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mBaseBinding.toolBar.setTitle(text);
        mBaseBinding.titleTv.setText("");
    }

    /**
     * 标题栏右上角图标设置
     *
     * @param iconRes
     */
    public void setRightBtn(int iconRes) {
        mBaseBinding.titlebarRightTv.setVisibility(View.VISIBLE);
        mBaseBinding.titlebarRightTv.setBackgroundDrawable(getResources().getDrawable(iconRes));
    }

    /**
     * 点击右侧按钮
     *
     * @param view
     */
    public void titleRightClick(View view) {
        rightClick();
    }

    protected void rightClick() {

    }

    /**
     * 点击左侧按钮
     *
     * @param view
     */
    public void titleLeftClick(View view) {
        leftClick();
    }

    protected void leftClick() {

    }
    public void setIsRotate(boolean isRotate) {
        this.mIsRotate = isRotate;
    }

    public boolean getIsRotate() {
        return mIsRotate;
    }
    public boolean hasPermission(String permission) {
        return PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(mContext, permission);
    }


    public void requestPeermission(String[] permissions, int requestCode) {
        ActivityCompat.requestPermissions(this, permissions, requestCode);
    }
    protected void showSnackbar(String str) {
        Snackbar.make(findViewById(android.R.id.content), str, Snackbar.LENGTH_LONG).show();
    }
    protected void initState() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);

            int navigationBarHeight = Utils.getNavigationBarHeight(this);
            getWindow().getDecorView().findViewById(android.R.id.content).
                    setPadding(0, 0, 0, navigationBarHeight);


        }
    }
}
